<template>
  <!-- 可折叠表格 -->
  <el-table
    style="width: 100%; margin-bottom: 20px"
    default-expand-all
    row-key="id"
    border
    :data="permissionArr"
  >
    <el-table-column label="名称" prop="name"></el-table-column>
    <el-table-column label="权限值" prop="code"></el-table-column>
    <el-table-column label="修改时间" prop="updateTime"></el-table-column>
    <el-table-column label="操作">
      <!-- row为每一个已有的菜单对象|按钮对象 -->
      <template #="{ row, $index }">
        <el-button
          type="primary"
          size="small"
          :disabled="row.level == 4 ? true : false"
          @click="addPermission(row)"
        >
          {{ row.level == 3 ? '添加功能' : '添加菜单' }}
        </el-button>
        <el-button
          type="primary"
          size="small"
          :disabled="row.level == 1 ? true : false"
          @click="updatePermission(row)"
        >
          编辑
        </el-button>
        <el-popconfirm
          width="260px"
          :title="`确定删除${row.name}吗?`"
          @confirm="removePermission(row)"
        >
          <template #reference>
            <el-button
              type="primary"
              size="small"
              :disabled="row.level == 1 ? true : false"
            >
              删除
            </el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <!-- 对话框：添加或更新已有菜单的数据的结构 -->
  <el-dialog
    v-model="dialogVisible"
    :title="menuData.id ? '更新菜单' : '添加菜单'"
  >
    <!-- 表单：收集新增和已有菜单的数据 -->
    <el-form>
      <el-form-item label="名称">
        <el-input
          placeholder="请输入菜单名称"
          v-model="menuData.name"
        ></el-input>
      </el-form-item>
      <el-form-item label="权限">
        <el-input
          placeholder="请输入权限数值"
          v-model="menuData.code"
        ></el-input>
      </el-form-item>
    </el-form>
    <!-- 插槽：确定和取消按钮 -->
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import {
  reqAllPermission,
  reqAddOrUpdateMenu,
  reqRemoveMenu,
} from '@/api/acl/menu'
import type {
  PermisstionResponseData,
  Permisstion,
  MenuParams,
} from '@/api/acl/menu/type'
import { ElMessage } from 'element-plus'

//存储所有菜单权限数组
let permissionArr = reactive<Permisstion[]>([])
//控制对话框的显示和隐藏
let dialogVisible = ref<boolean>(false)
//携带参数
let menuData = reactive<MenuParams>({
  level: 0, //几级菜单
  pid: 0, //菜单的Id
  name: '', //菜单的名字
  code: '', //权限数值
})

//发送请求获取所有菜单权限数据的方法
const getHasPermission = async () => {
  ////发送请求获取所有菜单权限数据
  let result: PermisstionResponseData = await reqAllPermission()
  if (result.code == 200) {
    permissionArr = result.data
  }
}
//组件挂载完毕：发送请求获取所有菜单权限数据
onMounted(() => {
  //发送请求获取所有菜单权限数据
  getHasPermission()
})
//点击添加菜单按钮的回调
const addPermission = (row: Permisstion) => {
  //清空参数（在表单v-model中收集name code）
  Object.assign(menuData, {
    id: 0, //这个别忘了清空！！！
    pid: 0, //菜单的ID
    level: 0, //几级菜单
    name: '', //菜单的名字
    code: '', //权限数值
  })
  //收集参数：pid lever
  menuData.pid = row.id as number
  menuData.level = row.level + 1
  //显示对话框
  dialogVisible.value = true
}
//点击更新菜单的回调
const updatePermission = (row: Permisstion) => {
  //数据回显（在表单v-model中收集name code） + 收集参数：pid lever
  Object.assign(menuData, row)
  //显示对话框
  dialogVisible.value = true
}
//点击对话框确定按钮的回调
const save = async () => {
  //发送请求新增子菜单|更新已有菜单
  let result: any = await reqAddOrUpdateMenu(menuData)
  if (result.code == 200) {
    //隐藏对话框
    dialogVisible.value = false
    //提示信息
    ElMessage({
      type: 'success',
      message: menuData.id ? '更新成功' : '添加成功',
    })
    //再次发送请求获取所有菜单权限数据
    getHasPermission()
  }
}
//点击删除按钮的回调
const removePermission = async (row: Permisstion) => {
  //发送请求删除某一个id的菜单或按钮权限
  let result: any = await reqRemoveMenu(row.id as number)
  if (result.code == 200) {
    //提示消息
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    //重新发送请求获取所有菜单权限数据
    getHasPermission()
  }
}
</script>

<style scoped lang="scss"></style>
